package org.springblade.common.utils.mq;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.PropertyValueConst;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.thierrysquirrel.annotation.MessageListener;
import com.github.thierrysquirrel.annotation.RocketListener;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.config.ProjectApiConfiguration;
import org.springblade.common.constant.MaintenanceSmsConstant;
import org.springblade.common.constant.TemplateMsgConstant;
import org.springblade.common.constant.VoiceConstant;
import org.springblade.common.utils.AsyncHttpUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.repair.dto.SimpleElevatorPlanInfoDTO;
import org.springblade.modules.repair.entity.BusinessApprove;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.entity.MaintainPlanContentUser;
import org.springblade.modules.repair.enums.ApproveStatusEnum;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.service.*;
import org.springblade.modules.repair.vo.ElevatorSimpleInfoVO;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.SimpleUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Jackey
 * Date: 2022/05/09
 * Time: 下午2:03
 * Description:	报障延迟队列接受
 */
@Slf4j
@Component
@RocketListener(groupID = "GID_delayed",messageModel = PropertyValueConst.BROADCASTING)
public class AlarmReceive {

	@Autowired
	private BusinessApproveService businessApproveService;

	@Autowired
	private WeChatDialogueService weChatDialogueService;

	@Autowired
	private IMaintainSignService maintainSignService;

	@Autowired
	private ProjectApiConfiguration projectApiConfiguration;

	@Autowired
	private IMaintainPlanContentService planContentService;

	@Autowired
	private IMaintainPlanContentUserService planContentUserService;

	@Autowired
	private IUserService userService;

	@Autowired
	private AsyncHttpUtil asyncHttpUtil;

	/**
	 * 报障消费延迟队列消息
	 * @param message 消息体
	 */
    @MessageListener(topic = "delayed", tag = "alarm")
    public void receiveMessage(String message) {
		JSONObject jsonObject = JSONObject.parseObject(message);
		Long businessApproveId = jsonObject.getLongValue("businessApproveId");
		BusinessApprove businessApprove = businessApproveService.getById(businessApproveId);
		if (businessApprove == null || businessApprove.getApproveStatus() != ApproveStatusEnum.PENDING.getId()) {
			return;
		}

		JSONArray usersArr = jsonObject.getJSONArray("approveUsers");
		List<SimpleUserVO> approveUsers = usersArr.toJavaList(SimpleUserVO.class);
		Map<String, String> templateDataMap = jsonObject.getObject("templateDataMap", Map.class);
		for (SimpleUserVO approveUser : approveUsers) {
			if (Func.isNotEmpty(approveUser.getOpenid())) {
				log.info("===========给经理发送模板消息,审批人:[{}]===========", approveUser.getUserName());
				weChatDialogueService.sendMsg(approveUser.getOpenid(), TemplateMsgConstant.T_DEVICE_FAULT_ID, templateDataMap);
			}

			log.info("===========给经理发送手机短信,审批人:[{}], 手机号码:[{}]===========", approveUser.getUserName(), approveUser.getPhone());
			Map<String, String> smsParam = new HashMap<>(2);
			smsParam.put("auditType", "【待审批】");
			smsParam.put("auditPosition", "【报障审批】");
			SmsUtil.sendMessage(MaintenanceSmsConstant.MAINTENANCE_AUDIT, BladeConstant.ADMIN_TENANT_ID, smsParam, approveUser.getPhone());

			log.info("===========给经理打电话,审批人:[{}], 手机号码:[{}]===========", approveUser.getUserName(), approveUser.getPhone());
			Map<String, String> phoneMap = new HashMap<>(4);
			phoneMap.put("place", templateDataMap.get("keyword1") + templateDataMap.get("keyword2"));
			phoneMap.put("phone", approveUser.getPhone());
			phoneMap.put("templateCode", VoiceConstant.MAINTENANCE_CHECK_USER);
			asyncHttpUtil.createPost(projectApiConfiguration.getWeb()+"/admin/voice/sendPhone",JSONObject.toJSONString(phoneMap));
		}
    }

	@MessageListener(topic = "delayed", tag = "sign_out")
    public void receiveWxPartsSignOutMessage(String message) {
		log.info("===========接收到系统代签消息，内容:[{}]===========", message);
		JSONObject jsonObject = JSONObject.parseObject(message);
		Long maintainSignId = jsonObject.getLongValue("maintainSignId");
		Long planId = jsonObject.getLongValue("planId");
		Integer typeStatus = jsonObject.getIntValue("typeStatus");
		maintainSignService.wxPartsAutoSignOut(maintainSignId, planId, typeStatus);
	}



}


























